package cn.fql._04trans;

import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionMQProducer;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.common.message.Message;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Producer {
    public static void main(String[] args) throws Exception {
        //1.先创建一个生产者对象
        TransactionMQProducer producer = new TransactionMQProducer("trans-producer");
        //2.设置nameserver 地址
        producer.setNamesrvAddr("127.0.0.1:9876");
        //线程池底层是使用新开线程去发布消息到MQ
        ExecutorService excutorService = Executors.newFixedThreadPool(20);

        producer.setExecutorService(excutorService);
        //添加事务监听
        producer.setTransactionListener(new MyTransactionListenerImpl());
        //3.启动生产者
        producer.start();
        // 创建一个消息对象
        Message msg = new Message("trans-topic", "trans-tags", "阿巴阿巴".getBytes("UTF-8"));
        //4.发送消息
        TransactionSendResult send = producer.sendMessageInTransaction(msg, null);
        //单向消息--没有返回值【可靠性最低，效率最高】
        //producer.sendOneway(msg);
        System.out.println(send);
        //5.使用完毕之后 关闭实例
        producer.shutdown();

    }
}
